# This code is part of the MWP System
# Copyright (c) 2012 Benjamin Lubin (blubin@bu.com) 
# Published under and subject to the GPLv2 license available at http://www.gnu.org/licenses/gpl-2.0.html

'''
Created on Dec 13, 2012
@author: blubin
'''

from backend.backend import Backend;
from workflow.task import Task;
from workflow.result import Result;
from workflow.flowData import Status;

class DrugBackend(Backend):
   
    def __init__(self): 
        Backend.__init__(self, "DrugDiscovery", dashboard=True);

    def wire(self):
        self.register_result_listener("Screener", "HTS", self.screen_complete)
        self.register_result_listener("Chemist", "Synth", self.synth_complete)
        self.register_result_listener("Ephys", "InVitro", self.invitro_complete)
        self.register_result_listener("Pharma", "InVivo", self.invivo_complete)
        self.register_result_listener("Regulatory", "File", self.file_complete)
        self.register_result_listener("SuperSecretSuperSafe", "Catalog", self.catalog_complete)
        
        
#       self.register_joined_listener([("Pharmacist", "FillPrescription", Result), ("Physician", "IsPrescriptionNeeded", Result, Status.COMPLETE)], self.fill_prescription_predicate, self.fill_prescription_complete)

    def screen_complete(self, results):
        for Result in results:
            if Result.data['isscreenapproved'] == "TRUE":
                task = Task.construct_from_result(Result, "Chemist", "Synth");
            else:
                task = Task.construct_from_result(Result, "SuperSecretSuperSafe", "Catalog");
            self.workflow.add(task);
            self.workflow.update_status(Result, Status.COMPLETE);

    def synth_complete(self, results):
        for Result in results:
            task = Task.construct_from_result(Result, "Ephys", "InVitro");
            self.workflow.add(task);
            self.workflow.update_status(Result, Status.COMPLETE);

    def invitro_complete(self, results):
            for Result in results:
                if Result.data['isinvitroapproved'] == "TRUE":
                    task = Task.construct_from_result(Result, "Pharma", "InVivo");
                else:
                    task = Task.construct_from_result(Result, "SuperSecretSuperSafe", "Catalog");
                self.workflow.add(task);
                self.workflow.update_status(Result, Status.COMPLETE);

    def invivo_complete(self, results):
            for Result in results:
                if Result.data['isinvivoapproved'] == "TRUE":
                    task = Task.construct_from_result(Result, "Regulatory", "File");
                else:
                    task = Task.construct_from_result(Result, "SuperSecretSuperSafe", "Catalog");
                self.workflow.add(task);
                self.workflow.update_status(Result, Status.COMPLETE);

    def file_complete(self, results):
            for Result in results:
                self.workflow.update_status(Result, Status.COMPLETE);

    def catalog_complete(self, results):
            for Result in results:
                self.workflow.update_status(Result, Status.COMPLETE);


if __name__ == '__main__':
    backend = DrugBackend();